package com.hzy.pay.wxpay.util;

import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import org.apache.commons.lang3.StringUtils;

/**
 * Client Util
 * 
 * @author zhuowei.luo
 * @date 2018/7/9
 */
public class WxpayClientUtil {

	private static WxPayService client;

	static {
		init();
	}

	public static void reInit() {
		init();
	}
	
	/**
	 * 获取默认 WxPayService
	 */
	public static WxPayService getDefaultWxpayClient() {
		if (client == null) {
			init();
		}
		return client;
	}

	private static void init() {
		client = new WxPayServiceImpl();
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(StringUtils.trimToNull(WeixinpayConfigUtil.appId));
        payConfig.setMchId(StringUtils.trimToNull(WeixinpayConfigUtil.mchId));
        payConfig.setMchKey(StringUtils.trimToNull(WeixinpayConfigUtil.mchId));
        payConfig.setKeyPath(StringUtils.trimToNull(WeixinpayConfigUtil.apiKey));
        payConfig.setNotifyUrl(StringUtils.trimToNull(WeixinpayConfigUtil.notifyUrl));
        // 可以指定是否使用沙箱环境
        payConfig.setUseSandboxEnv(false);
        client.setConfig(payConfig);;
	}

}
